﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FisheryPlatform.Search.Core.Analysis
{
    /// <summary>
    /// FWLLexicon: Fixed word length lexicon, without index.
    /// </summary>
    /// <author>gwd, 2006-6-7</author>
    public class FWLLexicon : Lexicon
    {
        private int _wordLength;      // the word length of the Lexicon
        private string _fullPath;     // lexicon data file full path
        private System.Collections.Hashtable _entries = null;      // the memory Hashtable to store lexicon data items

        public FWLLexicon(Dictionary dic, int wordLength)
            : base(dic)
        {
            _entries = new System.Collections.Hashtable();

            _wordLength = wordLength;

            string fullPath = dic.FullPath + System.IO.Path.DirectorySeparatorChar.ToString() + Lexicon.LEXICON_PREFIX + _wordLength + ".";
            _fullPath = fullPath + Lexicon.LEXICON_EXT;
        }

        #region LoadLexiconIndex

        /// <summary>
        /// Do nothing. Do not need Lexion index in NormalLexicon.
        /// </summary>
        internal override void LoadLexiconIndex()
        {
            return;
        }

        #endregion LoadLexiconIndex

        #region LoadLexicon

        /// <summary>
        /// Load Lexicon data items into the memory Hashtabel from the lexicon data file.
        /// </summary>
        internal override void LoadLexicon()
        {
            if (!System.IO.File.Exists(_fullPath))
            {
                //System.Console.Out.WriteLine("Lexicon File not exists: " + _fullPath);
                return;
            }

            try
            {
                System.IO.StreamReader reader = null;
                try
                {
                    reader = new System.IO.StreamReader(_fullPath, MMSupport.GetEncoding());
                    string line;
                    while ((line = reader.ReadLine()) != null)
                    {
                        this.ParseLexiconEntry(line);
                    }
                }
                finally
                {
                    if (reader != null) reader.Close();
                }
            }
            catch (System.IO.FileNotFoundException e)
            {
                System.Console.Out.WriteLine("Cannot find Lexicon File: " + _fullPath);
                System.Console.Error.WriteLine(e.ToString());
            }
            catch (System.IO.IOException e)
            {
                System.Console.Out.WriteLine("Cannot read Lexicon File: " + _fullPath);
                System.Console.Error.WriteLine(e.ToString());
            }
        }

        protected internal virtual void ParseLexiconEntry(string entry)
        {
            string word = entry.Substring(0, _wordLength);
            string freq = entry.Substring(_wordLength).Trim();

            _entries[word] = freq;      // Pay attention to the difference between Hashtable.Add(key, value) and Hashtable[key]=value.
        }

        #endregion LoadLexicon

        /// <summary>
        /// See Lexicon.GetEntry.
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        internal override object GetEntry(string key)
        {
            return _entries[key];
        }

        /// <summary>
        /// See Lexicon.GetWordFreq.
        /// </summary>
        /// <param name="word"></param>
        /// <returns></returns>
        internal override long GetWordFreq(string word)
        {
            return long.Parse(_entries[word].ToString());
        }

    }
}
